﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
namespace BenefitHelper.Data.Day
{
    /// <summary>
    /// 盈利或亏损所占的Tick
    /// </summary>
    public class DayWinOrLoseTick
    {
        /// <summary>
        /// 编号
        /// </summary>
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 日期
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 日期
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }
        /// <summary>
        /// 是否亏损
        /// </summary>
        public bool Win { get; set; }
        /// <summary>
        /// 发生TICK值
        /// </summary>
        public int Tick { get; set; }
        /// <summary>
        /// 次数
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 合约编号
        /// </summary>
        public int InstrumentId { get; set; }
        /// <summary>
        /// 合约
        /// </summary>
        public virtual Instrument Instrument { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }


        /// <summary>
        /// 得出超出止损次数
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="serverAccountId"></param>
        /// <param name="stopLoseTick"></param>
        /// <returns></returns>
        public int GetOverStopLoss(string pdate, int serverAccountId, int stopLoseTick, DB.DBManager db)
        {
            int count = 0;
            var query = from t in db.DayWinOrLoseTick where t.Win == false && t.Tick >= stopLoseTick select t;//zhege?
            if (query.Count() > 0)
            {
                count = query.Sum(a => a.Count);
            }
            return count;
        }

        /// <summary>
        /// 得出超出止损次数
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="serverAccountId"></param>
        /// <param name="stopLoseTick"></param>
        /// <returns></returns>
        public int GetInstrumentOverStopLoss(string pdate, int serverAccountId, int stopLoseTick, int instrumentid, int direction, DB.DBManager db)
        {
            int count = 0;
            var query = from t in db.DayWinOrLoseTick where t.Win == false && t.Tick >= stopLoseTick && t.InstrumentId == instrumentid && t.Direction == direction select t;//zhege?
            if (query.Count() > 0)
            {
                count = query.Sum(a => a.Count);
            }
            return count;
        }

        /// <summary>
        /// 得出超出止损次数
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="serverAccountId"></param>
        /// <param name="stopLoseTick"></param>
        /// <returns></returns>
        public int GetProductOverStopLoss(string pdate, int serverAccountId, int stopLoseTick, int productid, int direction, DB.DBManager db)
        {
            int count = 0;
            var query = from t in db.DayWinOrLoseTick where t.Win == false && t.Tick >= stopLoseTick && t.Instrument.ProductId == productid && t.Direction == direction select t;//zhege?
            if (query.Count() > 0)
            {
                count = query.Sum(a => a.Count);
            }
            return count;
        }

        /// <summary>
        /// 得出品种胜率
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="serverAccountId"></param>
        /// <param name="stopLoseTick"></param>
        /// <returns></returns>
        public double GetProductWinRate(string pdate, int serverAccountId, int productid, int direction, DB.DBManager db)
        {
            int Wincount = 0;
            int Lostcount = 1;
            var query = from t in db.DayWinOrLoseTick where t.Win == false && t.Win == false && t.Instrument.ProductId == productid && t.Direction == direction select t;
            if (query.Count() > 0)
            {
                Wincount = query.Count();
            }

            var query2 = from t in db.DayWinOrLoseTick where t.Win == false && t.Win == true && t.Instrument.ProductId == productid && t.Direction == direction select t;
            if (query2.Count() > 0)
            {
                Lostcount = query2.Count();
            }
            return Wincount / Lostcount;
        }


        /// <summary>
        /// 初始化Tick值
        /// </summary>
        /// <param name="pdate"></param>
        /// <param name="tradeHistoryId"></param>
        public void InitTickValue(string pdate, int tradeHistoryId, DB.DBManager db)
        {
            int dayTickTableCount1 = db.DayWinOrLoseTick.Count();

            var query = (from t in db.ClosePositionDetail where t.TradingDay.Equals(pdate) select t).ToList();
            List<Data.ServerData.T_Instrument> instruments = new Data.ServerData.T_Instrument().GetList();
            List<Data.ServerData.T_Account> serverAccounts = new List<ServerData.T_Account>();
            int connectionStringCount = System.Configuration.ConfigurationManager.ConnectionStrings.Count;
            for (int i = 0; i < connectionStringCount; i++)
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                if (System.Configuration.ConfigurationManager.ConnectionStrings[i].ProviderName.Equals("System.Data.OracleClient"))
                {
                    Common.ADO.Tools.OracleHelper.connectionString = connectionString;
                    List<BenefitHelper.Data.ServerData.T_Account> accountList = new BenefitHelper.Data.ServerData.T_Account().GetList();
                    foreach (BenefitHelper.Data.ServerData.T_Account _account in accountList)
                    {
                        serverAccounts.Add(_account);
                    }
                }
            }

            foreach (Data.ServerData.T_ClosePositionDetail_History h in query)
            {
                double winPrice = h.TradePrice - h.OpenPrice;
                var _query = from t in instruments where t.InstrumentID.Equals(h.InstrumentId) select t;
                if (_query.Count() > 0)
                {
                    double tick = _query.First().PriceTick;
                    int allTick = Convert.ToInt32(winPrice / tick);
                    DayWinOrLoseTick dayTick = new DayWinOrLoseTick();
                    if (winPrice > 0)
                    {
                        dayTick.Win = true;
                    }
                    else
                    {
                        dayTick.Win = false;
                    }
                    dayTick.TradeHistoryId = tradeHistoryId;
                    dayTick.Tick = Math.Abs(allTick);
                    dayTick.InstrumentId = new Instrument().GetInstrumentId(h.InstrumentId, db);
                    dayTick.Direction = Convert.ToInt32(h.Direction);
                    int loginAccountId = h.AccountId;
                    var getAccount = from t in serverAccounts where t.Id == loginAccountId select t;
                    Sys.Account account = new Sys.Account();
                    dayTick.AccountId = account.GetAccountId(getAccount.First().LoginAccount);
                    var countTick = from t in db.DayWinOrLoseTick where t.AccountId == dayTick.AccountId && t.Tick == dayTick.Tick && t.TradeHistoryId == dayTick.TradeHistoryId && t.Win == dayTick.Win && t.InstrumentId == dayTick.InstrumentId && t.Direction == dayTick.Direction select t;
                    if (countTick.Count() > 0)
                    {
                        dayTick = countTick.First();
                        dayTick.Count++;
                        db.SaveChanges();
                    }
                    else
                    {
                        dayTick.Count = 1;
                        db.DayWinOrLoseTick.Add(dayTick);
                        db.SaveChanges();
                    }
                }
            }
        }
        /// <summary>
        /// 删除某天所有信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = from t in db.DayWinOrLoseTick where t.TradeHistoryId == tradeHistoryId select t;
            foreach (DayWinOrLoseTick t in query)
            {
                db.DayWinOrLoseTick.Remove(t);
            }
            db.SaveChanges();
        }
    }
}